package fm.dongman.animefm.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.jetbrains.annotations.NotNull;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.contract.CommentEditContract;
import fm.dongman.animefm.view.util.ToastHelper;
import fm.dongman.modellib.StaticArea.Injection;
import fm.dongman.modellib.model.CommentModel;
import fm.dongman.presenterlib.presenter.CommentEditPresenter;

/**
 * Created by alex on 2017/10/18.
 *
 * @description 带有一个EditText的dialog
 */

public class CommentEditTextDialog extends Dialog {

    private CommentEditTextDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements CommentEditContract.ICommentEditView {
        private String mTitle;
        private Context mContext;
        private OnButtonClickListener mListener;
        private CommentEditContract.ICommentEditPresenter mPresenter;
        private CommentEditTextDialog mDialog;
        private String mType;
        private String mSubjectId;
        private String inputStr;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setOnButtonClickListenr(OnButtonClickListener listener) {
            mListener = listener;
            return this;
        }

        /**
         * 设置评论类型
         *
         * @param type 类型 详情参见CommentModel
         * @return
         */
        public Builder setCommentType(String type) {
            mType = type;
            return this;
        }

        public Builder setSubjectId(String id) {
            mSubjectId = id;
            return this;
        }

        public CommentEditTextDialog build() {
            if (mType == null)
                throw new RuntimeException("必须设置被评论的对象类型！");
            if (mSubjectId == null)
                throw new RuntimeException("必须设置被评论对象的id！");

            new CommentEditPresenter(this, Injection.provideCommentEditRepository());

            mDialog = new CommentEditTextDialog(mContext, R.style.CustomDialog);
            View layoutView = View.inflate(mContext, R.layout.dialog_edit_text, null);
            mDialog.setContentView(layoutView);
            TextView title = (TextView) layoutView.findViewById(R.id.mTvEditDialogTitle);
            if (mTitle != null)
                title.setText(mTitle);
            final EditText input = (EditText) layoutView.findViewById(R.id.mTvEditDialogEditText);
            Button cancel = (Button) layoutView.findViewById(R.id.mBtnEditDialogCancel);
            Button confirm = (Button) layoutView.findViewById(R.id.mBtnEditDialogConfirm);

            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                    if (mListener != null)
                        mListener.onCanceled();
                }
            });

            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    inputStr = input.getText().toString().trim();
                    if (inputStr.length() == 0) {
                        ToastHelper.getInstance(mContext).showWarn("内容不能为空");
                        return;
                    }
                    mDialog.dismiss();
                    if (mListener != null)
                        mListener.onCommit(inputStr);
                    CommentModel commentModel = new CommentModel();
                    commentModel.setContent(input.getText().toString());
                    commentModel.setType(mType);
                    commentModel.setSubjectId(mSubjectId);
                    mPresenter.submitComment(commentModel);
                }
            });

            return mDialog;
        }

        @Override
        public void showSubmitSucceed() {
            ToastHelper.getInstance(mContext).showSucceed("评论成功");
            mDialog.dismiss();
            if (mListener != null)
                mListener.onCommitSucceed();
        }

        @Override
        public void showSubmitFailed() {
            ToastHelper.getInstance(mContext).showSucceed("评论失败");
            mDialog.dismiss();
            if (mListener != null)
                mListener.onCommitFailed();
        }

        @Override
        public void setPresenter(CommentEditContract.ICommentEditPresenter iCommentEditPresenter) {
            mPresenter = iCommentEditPresenter;
        }

        @Override
        public boolean isActive() {
            return true;
        }

        @NotNull
        @Override
        public Context getViewContext() {
            return mContext;
        }

        @Override
        public void showNoNetwork() {

        }

        @Override
        public void showRefreshFailed() {

        }

        public interface OnButtonClickListener {
            void onCommit(String inputContent);

            void onCanceled();

            void onCommitSucceed();

            void onCommitFailed();

        }
    }
}
